package es

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"strings"
	"week2-project/model/mysql"
)

func HeightLight(data mysql.Video) EsRequest {
	es := EsDb
	video := map[string]interface{}{
		"query": map[string]interface{}{
			"match": map[string]interface{}{
				"VideoTitle": data,
			},
		},
		"highlight": map[string]interface{}{
			"fields": map[string]interface{}{
				"VideoTitle": map[string]interface{}{
					"pre_tags":  "<em>",
					"post_tags": "</em>",
				},
			},
		},
	}
	marshal, _ := json.Marshal(video)
	res, err := es.Search(
		es.Search.WithContext(context.Background()),
		es.Search.WithIndex("video_title"),
		es.Search.WithBody(strings.NewReader(string(marshal))),
		es.Search.WithTrackTotalHits(true),
		es.Search.WithPretty(),
	)
	fmt.Println(video)
	if err != nil {
		log.Fatalf("ERROR: %s", err)
	}
	defer res.Body.Close()
	fmt.Println("111", res)
	var videoReq EsRequest
	log.SetFlags(0)
	return videoReq

}

type EsRequest struct {
	Took     int  `json:"took"`
	TimedOut bool `json:"timed_out"`
	Shards   struct {
		Total      int `json:"total"`
		Successful int `json:"successful"`
		Skipped    int `json:"skipped"`
		Failed     int `json:"failed"`
	} `json:"_shards"`
	Hits struct {
		Total struct {
			Value    int    `json:"value"`
			Relation string `json:"relation"`
		} `json:"total"`
		MaxScore float64 `json:"max_score"`
		Hits     []struct {
			Index  string  `json:"_index"`
			Type   string  `json:"_type"`
			Id     string  `json:"_id"`
			Score  float64 `json:"_score"`
			Source struct {
				Id         int    `json:"id"`
				VideoTitle string `json:"VideoTitle"`
				GoodsStock int    `json:"goods_stock"`
			} `json:"_source"`
			Highlight struct {
				VideoTitle []string `json:"VideoTitle"`
			} `json:"highlight"`
		} `json:"hits"`
	} `json:"hits"`
}
